<template>
    <el-aside :width="width">
        <el-menu background-color="#545c64" text-color="#fff" class="el-menu-vertical-demo" :collapse="isCollapse"
            :collapse-transition="false" :default-active="activeMenu">
            <h3 v-show="!isCollapse">后台管理系统</h3>
            <h3 v-show="isCollapse">后台</h3>
            <!-- 一级菜单（没有子菜单） -->
            <el-menu-item v-for="item in noChildren" :key="item.path" :index="item.path" @click="handleMenu(item)">
                <component class="icons" :is="item.icon"></component>
                <span>{{ item.label }}</span>
            </el-menu-item>
            <!-- 多级菜单（有孩子） -->
            <el-sub-menu v-for="item in hasChildren" :key="item.path" :index="item.path">
                <template #title>
                    <component class="icons" :is="item.icon"></component>
                    <span>{{ item.label }}</span>
                </template>
                <el-menu-item v-for="(subItem, subIndex) in item.children" :key="subItem.path" :index="subItem.path"
                    @click="handleMenu(subItem)">
                    <component class="icons" :is="subItem.icon"></component>
                    <span>{{ subItem.label }}</span>
                </el-menu-item>
            </el-sub-menu>
        </el-menu>
    </el-aside>
</template>

<script setup lang="js">
import { ref, computed } from 'vue'
import { useAllDataStore } from '@/store'
import { useRoute, useRouter } from 'vue-router';

const store = useAllDataStore()
const list = computed(() => store.state.menuList)
const noChildren = computed(() => list.value.filter(item => !item.children))
const hasChildren = computed(() => list.value.filter(item => item.children))
const isCollapse = computed(() => store.state.isCollapse)
const width = computed(() => store.state.isCollapse ? '64px' : '180px') //true折叠，false不折叠
const router = useRouter()
const route = useRoute()
const activeMenu = computed(() => route.path)
//点击菜单
const handleMenu = (item) => {
    router.push(item.path)//跳转至对应页面
    store.selectMenu(item)//调整tags
}
</script>

<style scoped lang="less">
.icons {
    width: 18px;
    height: 18px;
    margin-right: 5px;
}

.el-menu {
    border-right: none;

    h3 {
        line-height: 48px;
        color: #fff;
        text-align: center; //垂直居中
    }
}

.el-aside {
    height: 100%;
    background-color: #545c64;
}
</style>
